package hot.job.autoconfigure.configurer;

import hot.job.autoconfigure.scanner.HotJobScanner;
import hot.job.core.annotation.HotJob;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.lang.NonNull;
import org.springframework.util.StringUtils;

/**
 * hot job scanner configurer
 * only scans, don't do bean definition post process
 * @author gaowenhao
 * @since 2022/7/11 21:12
 */
public class JobScannerConfigurer implements BeanDefinitionRegistryPostProcessor {

    private String basePackages;

    public void setBasePackages(String basePackages) {
        this.basePackages = basePackages;
    }


    @Override
    public void postProcessBeanDefinitionRegistry(@NonNull BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
        HotJobScanner scanner = new HotJobScanner(beanDefinitionRegistry, false);
        scanner.addIncludeFilter(new AnnotationTypeFilter(HotJob.class));
        scanner.scan(StringUtils.tokenizeToStringArray(this.basePackages, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));
    }

    @Override
    public void postProcessBeanFactory(@NonNull ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        // left intentionally blank
    }

}
